from django.contrib.syndication.views import Feed
from django.utils.feedgenerator import Atom1Feed, Rss201rev2Feed
from apps.course.functions import course_reverse
from apps.course.models import Course

from apps.feeds import FeedItems

import logging
import datetime

logger = logging.getLogger('pet.apps.feeds.models')
	
class AnnouncementFeed(Feed):
	feed_type = Rss201rev2Feed

	def get_object(self, request, course=None):
		self.object = Course.find_or_404(course)
		return self.object
	def title(self, obj):
		return "%s '%s' Announcements and Events" % (obj.course_id, obj.course_title)
	def description(self, obj):
		return "Updates on the latest events and announcements for %s '%s'" % (obj.course_id, obj.course_title)
	def link(self, obj):
		from options import Options
		server_prefix = Options.get_option("Server Prefix","",app="General")
		#if not server_prefix.endswith('/'): server_prefix += '/'
		url = course_reverse('apps.news.views.index',obj.course_id)
		if server_prefix:
			url = server_prefix + url
		import sys; sys.stderr.write("link: %s\n"%url); sys.stderr.flush()
		return url
	
	def item_guid(self, item):
		return str(item.id)
	def item_link(self):
		return self.link(self.object)
	def item_title(self, item):
		return item.get_title()
	def item_description(self, item):
		return item.get_description()
	def item_pubdate(self, item):
		return item.get_appearance_date()
	
	def items(self):
		classes = FeedItems.get_items()['News']
		item_list = []
		for classname in classes:
				for item in classname.objects.all():
					if item.get_appearance_date() < datetime.datetime.now():
						item_list.append(item)
		item_list.sort(key=lambda item: max(item.get_appearance_date(),item.last_modified) if item.show_updates() else item.get_appearance_date())
		if len(item_list) > 25:
			item_list = item_list[:25]
		return item_list

class AtomAnnouncementFeed(AnnouncementFeed):
	feed_type = Atom1Feed
	subtitle = AnnouncementFeed.description
	
class SiteUpdateFeed(Feed):
	feed_type = Rss201rev2Feed

	def get_object(self, request, course=None):
		self.object = Course.find_or_404(course)
		return self.object
	def title(self, obj):
		return "%s '%s' Announcements and Website Updates" % (obj.course_id, obj.course_title)
	def description(self, obj):
		return "Updates on the latest changes and announcements for %s '%s'" % (obj.course_id, obj.course_title)
	def link(self, obj):
		from options import Options
		server_prefix = Options.get_option("Server Prefix","",app="General")
		#if not server_prefix.endswith('/'): server_prefix += '/'
		url = course_reverse('apps.news.views.index',obj.course_id)
		if server_prefix:
			url = server_prefix + url
		import sys; sys.stderr.write("link: %s\n"%url); sys.stderr.flush()
		return url
	def item_guid(self, item):
		return str(item.id)
		
	def item_link(self, item):
		import sys; sys.stderr.write("link: %s\n"%item.get_link(self.object.course_id)); sys.stderr.flush()
		return item.get_link(self.object.course_id)
	def item_title(self,item):
		return "%s %s %s" % (type(item).__name__,item.get_title(),'Created' if item.get_appearance_date() >= item.last_modified else "Updated")
	def item_description(self, item):
		return """The %s %s has been %s. %s""" % (type(item).__name__,
					item.get_title(), 'created' if item.get_appearance_date() >= item.last_modified else "updated",
					"<br>%s" % item.get_description() if item.get_description() else "")
	
	def items(self):
		classes = FeedItems.get_items()
		item_list = []
		for classlist in classes.values():
				for classname in classlist:
					for item in classname.objects.all():
						if item.get_appearance_date() < datetime.datetime.now():
							item_list.append(item)
		item_list.sort(key=lambda item: max(item.get_appearance_date(),item.last_modified) if item.show_updates() else item.get_appearance_date())
		if len(item_list) > 25:
			item_list = item_list[:25]
		return item_list
		
class AtomSiteUpdateFeed(SiteUpdateFeed):
	feed_type = Atom1Feed
	subtitle = SiteUpdateFeed.description